#!/bin/bash
#description install httpd server
#data 2016/5/2
#author 51yuki
#source function library

. /etc/rc.d/init.d/functions

function init_centos6() {
#configure yum source on centos6
cd /etc/yum.repos.d/ && [ -f Centos-6.repo ] || wget -O Centos-6.repo http://mirrors.aliyun.com/repo/Centos-6.repo && [ -f epel-6.repo ] || wget -O epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo

# Install the software required before compiling
yum -y install vim wget unzip cmake pcre pcre-devel readline-devel zlib-devel openssl-devel openssl-clients  gcc-c++ gcc boost boost-devel bison bison-devel tree
# Temporarily close the firewall and selinux
sed -ir 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config && setenforce 0
service iptables stop &>/dev/null
# create directory storage software
[ -d /usr/local/src ] || mkdir -p /usr/local/src && echo "the file directory is esxit" 
}
#configure yum source on centos7
function init_centos7() {
cd /etc/yum.repos.d/ && [ -f Centos-7.repo ] || wget -O Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo && [ -f epel-7.repo ] || wget -O epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

# Install the software required before compiling
yum -y install vim wget lbzip2 unzip cmake pcre pcre-devel readline-devel zlib-devel openssl-devel openssl-clients  gcc-c++ gcc boost boost-devel bison bison-devel tree net-tools
# Temporarily close the firewall and selinux
sed -ir 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config && setenforce 0
systemctl disable firewalld  &>/dev/null
systemctl stop firewalld
# create directory storage software
[ -d /usr/local/src ] || mkdir -p /usr/local/src && echo "the file directory is esxit" 
}
#install httpd2.2 on centos6
function http22_on_centos6() {
	init_centos6
	#download software
	WorkDir=/usr/local/src
	cd $WorkDir
	[ -f apr-1.5.2.tar.bz2 ] || wget http://archive.apache.org/dist/apr/apr-1.5.2.tar.bz2
	[ -f apr-iconv-1.2.1.tar.gz ] || wget http://archive.apache.org/dist/apr/apr-iconv-1.2.1.tar.gz	 
	[ -f apr-util-1.5.2.tar.bz2 ] || wget http://archive.apache.org/dist/apr/apr-util-1.5.2.tar.bz2
	[ -f httpd-2.2.34.tar.bz2 ] || wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.2.34.tar.bz2
	# install apr apr-util apr-iconv
	cd $WorkDir && tar xf apr-1.5.2.tar.bz2 && cd apr-1.5.2 && ./configure --prefix=/usr/local/apr && make && make install
	cd $WorkDir && tar xf apr-iconv-1.2.1.tar.gz && cd apr-iconv-1.2.1 && ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr && make && make install 
	cd $WorkDir && tar xf apr-util-1.5.2.tar.bz2 && cd apr-util-1.5.2 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install 
	#Compile the installation httpd2.2.34
	cd $WorkDir && tar xf  httpd-2.2.34.tar.bz2 && cd  httpd-2.2.34 && ./configure --prefix=/usr/local/http22  --sysconf=/etc/httpd22 --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv --with-apr-util=/usr/local/apr-util --with-zlib --with-pcre --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-mpm=worker -enable-modules=all && make && make install
	[ $? -eq 0 ] && echo "install httpd is success"
	#Simply configure HTTP services
<<<<<<< HEAD:install_httpd.sh
	cd /etc/httpd/conf && cp httpd.conf httpd.conf.bak
	echo "ServerName localhost:80" >> /etc/httpd/conf/httpd.conf
	 if [ "`netstat -lnt|grep 80|awk -F "[ :]+" '{print $4}'`" != "80" ];then
	     /usr/local/http22/bin/apachectl start	     
             action "httpd startup success" /bin/true
        else
             action "httpd is running" /bin/false
 fi
=======
	cd /etc/httpd22 && cp httpd.conf httpd.conf.bak
	echo "ServerName localhost:80" >> /etc/httpd22/httpd.conf
    if [ "`netstat -lnt|grep 80|awk -F "[ :]+" '{print $4}'`" != "80" ];then

             /usr/local/http22/bin/apachectl start
    else 
             echo -e "\033[0;32m httpd service is running\033[0m"
	     exit 1
    fi
>>>>>>> c371e5e47f5cc7c4f389c188edf873268286cbee:软件自动安装脚本/install_httpd.sh
}
function http2418_on_centos6() {
	init_centos6
	#download software
	WorkDir=/usr/local/src
	cd $WorkDir
	[ -f apr-1.6.3.tar.bz2 ] || wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.6.3.tar.bz2
	[ -f apr-iconv-1.2.2.tar.gz ] || wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-iconv-1.2.2.tar.gz 
	[ -f apr-util-1.6.1.tar.bz2 ] || wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
	[ -f httpd-2.4.18.tar.gz ] || wget http://soft.51yuki.cn/httpd-2.4.18.tar.gz
	# install apr apr-util apr-iconv
	cd $WorkDir && tar xf apr-1.6.3.tar.bz2 && cd apr-1.6.3 && ./configure --prefix=/usr/local/apr && make && make install
	cd $WorkDir && tar xf apr-iconv-1.2.2.tar.gz && cd apr-iconv-1.2.2 && ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr && make && make install 
	cd $WorkDir && tar xf apr-util-1.6.1.tar.bz2 && cd apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install 
	#Compile the installation httpd2.2.34
	cd $WorkDir && tar xf  httpd-2.4.18.tar.gz && cd  httpd-2.4.18  && ./configure --prefix=/usr/local/http24  --sysconf=/etc/httpd24 --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv --with-apr-util=/usr/local/apr-util --with-zlib --with-pcre --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork  && make && make install
	[ $? -eq 0 ] && echo "install httpd is success"
	#Simply configure HTTP services
	cd /etc/httpd24/ && cp httpd.conf httpd.conf.bak
	echo "ServerName localhost:80" >> /etc/httpd24/conf/httpd.conf
<<<<<<< HEAD:install_httpd.sh
	 if [ "`netstat -lnt|grep 80|awk -F "[ :]+" '{print $4}'`" != "80" ];then
             /usr/local/http24/bin/apachectl start
             action "httpd startup success" /bin/true
        else
             action "httpd is running" /bin/false
 	fi

=======
    	echo 'export PATH=$PATH:/usr/local/httpd22/bin' > /etc/profile.d/httpd.sh
        source /etc/profile.d/httpd.sh
 
    if [ "`netstat -lnt|grep 80|awk -F "[ :]+" '{print $4}'`" != "80" ];then

             apachectl start
    else 
             echo -e "\033[0;32m httpd service is running\033[0m"
	     exit 1
    fi
>>>>>>> c371e5e47f5cc7c4f389c188edf873268286cbee:软件自动安装脚本/install_httpd.sh
}
function http2429_on_centos7() {
        init_centos7
        #download software
        WorkDir=/usr/local/src
        cd $WorkDir
        [ -f apr-1.6.3.tar.bz2 ] || wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.6.3.tar.bz2
        [ -f apr-iconv-1.2.2.tar.gz ] || wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-iconv-1.2.2.tar.gz
        [ -f apr-util-1.6.1.tar.bz2 ] || wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.bz2
        [ -f httpd-2.4.29.tar.bz2 ] || wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.29.tar.bz2
        # install apr apr-util apr-iconv
        cd $WorkDir && tar xf apr-1.6.3.tar.bz2 && cd apr-1.6.3 && ./configure --prefix=/usr/local/apr && make && make install
        cd $WorkDir && tar xf apr-iconv-1.2.2.tar.gz && cd apr-iconv-1.2.2 && ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr && make && make install
        cd $WorkDir && tar xf apr-util-1.6.1.tar.bz2 && cd apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install
        #Compile the installation httpd2.2.34
        cd $WorkDir && tar xf  httpd-2.4.29.tar.bz2 && cd  httpd-2.4.29 && ./configure --prefix=/usr/local/http24  --sysconf=/etc/httpd24 --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv --with-apr-util=/usr/local/apr-util --with-zlib --with-pcre --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-mpm=worker -enable-modules=all && make && make install
        [ $? -eq 0 ] && echo "install httpd is success"
        #Simply configure HTTP services
        cd /etc/httpd24/conf && cp httpd.conf httpd.conf.bak
        echo "ServerName localhost:80" >> /etc/httpd24/conf/httpd.conf
 if [ "`netstat -lnt|grep 80|awk -F "[ :]+" '{print $4}'`" != "80" ];then
             /usr/local/http24/bin/apachectl start
             action "httpd startup success" /bin/true
        else
             action "httpd is running" /bin/false
 fi
}

function http22_yum_on_centos6(){

    cd /etc/yum.repos.d/ && [ -f Centos-6.repo ] || wget -O Centos-6.repo http://mirrors.aliyun.com/repo/Centos-6.repo && [ -f epel-6.repo ] || wget -O epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo
    yum -y install httpd httpd-devel mod_ssl openssl-devel  	
    cd /etc/httpd/conf && cp httpd.conf httpd.conf.bak
    echo "ServerName localhost:80" >> /etc/httpd/conf/httpd.conf
    if [ "`netstat -lnt|grep 80|awk -F "[ :]+" '{print $4}'`" != "80" ];then

	service httpd start 

    else
        echo -e "\033[0;32m httpd service is running\033[0m"
        exit 1
    fi

}
function http24_yum_on_centos7(){

    cd /etc/yum.repos.d/ && [ -f Centos-7.repo ] || wget -O Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo && [ -f epel-7.repo ] || wget -O epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo   
    yum -y install httpd httpd-devel mod_ssl openssl-devel
    cd /etc/httpd/conf && cp httpd.conf httpd.conf.bak
    echo "ServerName localhost:80" >> /etc/httpd/conf/httpd.conf
    if [ "`netstat -lnt|grep 80|awk -F "[ :]+" '{print $4}'`" != "80" ];then

       systemctl start httpd
    else
        echo -e "\033[0;32m httpd service is running\033[0m"
        exit 1
    fi

}
echo "##############################################################################"
	echo -e "\033[0;32m1、Compile the installation httpd-2.2.34 on centos6\033[0m"
	echo -e "\033[0;32m2、Compile the installation httpd-2.4.18 on centos6\033[0m"
	echo -e "\033[0;32m3、Compile the installation httpd-2.4.29 on centos7\033[0m"
	echo -e "\033[0;32m4、yum install http-2.2.15 on centos6\033[0m"
	echo -e "\033[0;32m5、yum install http-2.4.6 on centos7\033[0m"
echo "#############################################################################"
echo
echo
read -p "Please select the version to install: " NUM
case $NUM in
1)
   http22_on_centos6
   ;;
2)
  http2418_on_centos6
  ;;
3)
  http2429_on_centos7
  ;;
4)
 http22_yum_on_centos6
 ;;
5)
 http24_yum_on_centos7
 ;;
6)
 echo "Please input number"
 exit 2
esac
